using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace SCADAEditor.Component
{
    /// <summary>
    /// 按钮组件实现
    /// </summary>
    public class ButtonComponent : ComponentBase
    {
        // 移除原有的_text和_backColor字段
        // 移除原有的Text和BackColor属性
        
        public ButtonComponent()
        {
            Size = new Size(75, 23);
            Text = "Button"; // 设置默认文本
            IsEditMode = true;
        }
        
        public event Action<ButtonComponent> Click;
        
      
        
        public override void Draw(Graphics g)
        {
            // 绘制背景
            using (var brush = new SolidBrush(BackColor))
            {
                g.FillRectangle(brush, new Rectangle(Position, Size));
            }
            
            // 绘制边框
            using (var pen = new Pen(Color.Black))
            {
                g.DrawRectangle(pen, new Rectangle(Position, Size));
            }
            
            // 计算文本位置(居中)
            var textSize = g.MeasureString(Text, SystemFonts.DefaultFont);
            var textPos = new PointF(
                Position.X + (Size.Width - textSize.Width) / 2,
                Position.Y + (Size.Height - textSize.Height) / 2);
                
            // 绘制文本
            g.DrawString(Text, SystemFonts.DefaultFont, Brushes.Black, textPos);
            
            // 绘制编辑控制点
            DrawEditControls(g);
        }
        
        /// <summary>
        /// 处理点击检测
        /// </summary>
        public void CheckClick(Point clickPosition)
        {
            if (new Rectangle(Position, Size).Contains(clickPosition))
            {
                Click?.Invoke(this);
            }
        }
        
        public override Dictionary<string, object> ToJson()
        {
             var dict = base.ToJson();
            // 按钮组件没有特有属性需要保存
            return dict;

            // return new Dictionary<string, object>
            // {
            //     ["id"] = Id,
            //     ["name"] = Name,
            //     ["position"] = new { x = Position.X, y = Position.Y },
            //     ["size"] = new { width = Size.Width, height = Size.Height },
            //     ["text"] = Text,
            //     ["backColor"] = BackColor.ToArgb()
            // };
        }
        
        public override void FromJson(Dictionary<string, object> json)
        {
             base.FromJson(json);
           
            
            // 按钮组件没有特有属性需要加载
            // Id = json["id"].ToString();
            // Name = json["name"].ToString();
            // Position = new Point(
            //     Convert.ToInt32(((dynamic)json["position"]).x),
            //     Convert.ToInt32(((dynamic)json["position"]).y));
            // Size = new Size(
            //     Convert.ToInt32(((dynamic)json["size"]).width),
            //     Convert.ToInt32(((dynamic)json["size"]).height));
            // Text = json["text"].ToString();
            // BackColor = Color.FromArgb(Convert.ToInt32(json["backColor"]));
        }
        
        private void OnPropertyChanged(string propertyName)
        {
            // 可以在这里添加属性变更通知逻辑
        }
    }
}